var ERRORS_KEY = 'userErrors';

Template.userprofile.onCreated(function() {
	Session.set(ERRORS_KEY, {});
	// var email = $("#login-name-link").text().trim().replace(" ▾","");
	// console.log(email);
});
Template.userprofile.onRendered(function() {})

Template.userprofile.helpers({
	errorMessages: function() {
		return _.values(Session.get(ERRORS_KEY));
	},
	// departments: function() {
	// 	return Department.find({});
	// },
	// currUserEmail: function(){
	// 	return  Meteor.user().emails[0].address;
	// },
	// currUserProfile: function(){
	// 	console.log(Meteor.user())
	// 	return Meteor.user() ? Meteor.user().profile : {};
	// },
});

Template.userprofile.events({
	'submit': function(event, template) {
		event.preventDefault();
		var email = template.$('#email').val();
		var username = template.$('#username').val();
		var cellphone = template.$('#cellphone').val();
		var plateNO = template.$('#plateNO').val();
		var departmentId = template.$("#dept").val();
		var errors = {};

		if (!username) {
			errors.username = "username is required";
		};

		Session.set(ERRORS_KEY, errors);
		if (_.keys(errors).length) {
			return;
		}

		var status = Meteor.users.update({
			_id: Meteor.userId()
		}, {
			$set: {
				'profile.email': email,
				'profile.username': username,
				'profile.cellphone': cellphone,
				'profile.plateNO': plateNO,
				'profile.departmentId': departmentId,
			}
		})
		if (status) {
			Session.set('currStatus', "插入成功");
		};
		Meteor.setTimeout(function() {
			Session.set('currStatus', null);
		}, 3000);
	}
});